package util;

import com.alibaba.fastjson2.JSON;
import model.MaterialJson;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 材质文件数据工具类
 */
public class MaterialDataUtil {
	/**
	 * 解析材质文件 Material.json
	 *
	 * @param file 文件
	 * @return 解析后结果
	 */
	public static List<MaterialJson> parseMaterialFile(File file) {
		StringBuilder builder = new StringBuilder();
		try (FileReader fileReader = new FileReader(file, StandardCharsets.UTF_8);
		     BufferedReader br = new BufferedReader(fileReader)) {
			// encoding = fileReader.getEncoding(); //打包后这个值不是文件的
			String s;
			while ((s = br.readLine()) != null) {
				builder.append(s);
			}

			return JSON.parseArray(builder.toString()).stream()
					.map(o -> MaterialJson.parseJson(o.toString()))
					.toList();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
